package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

@WebServlet("/ScopedVarsServlet")
public class ScopedVarsServlet extends HttpServlet {
	
	private static final long serialVersionUID = 4800116405461965113L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setAttribute("attribute1", "First Value");
		HttpSession session = req.getSession();
		session.setAttribute("attribute2", "Second Value");
		ServletContext application = getServletContext();
		application.setAttribute("attribute3", new Date());
		
		//
		String repeatedAttr = "repeated";
		req.setAttribute(repeatedAttr, "Request Attr");
		session.setAttribute(repeatedAttr, "Session Attr");
		application.setAttribute(repeatedAttr, "Application Attr");
		//
		RequestDispatcher dispatcher = req.getRequestDispatcher("el/scoped-vars.jsp");
		dispatcher.forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}



	private static final Logger logger = 
			Logger.getLogger(ScopedVarsServlet.class);
}
